import routerList from "@/router"
import { useRoutes } from "react-router-dom"

/**
 * 获取你需要渲染路由表，动态加载
 * @returns 映射好的路由组件
 */
export default function DynamicRouter() {
    const userInfo = JSON.parse(localStorage.getItem("userInfo") || "{}")
    //如何判断一个对象是空对象
    if(Object.keys(userInfo) !=0){
        const {role:{menus}} = userInfo
        // ["user","role"]  实际 ["/home/user"]
        const userRouters = menus.map(item=>{
            return item.substr(6)
        })
        const index = routerList.findIndex(item=>item.path=="/home")
        let tempArray = [...routerList[index].children]
        tempArray = tempArray.filter(item=>{
            if(userRouters.includes(item.path)){
                return true
            }
        })
        routerList[index].children = tempArray
        console.log(tempArray);
    }
    //渲染
    const elements = useRoutes(routerList)
    return elements
}
